package com.abou.mvc.editor;

import java.beans.PropertyEditorSupport;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * The property editor used to convert commons multi part file values
 * 
 * @author vvq5150
 */
public class CommonsMultipartFileEditor extends PropertyEditorSupport {
  
  /**
   * The constructor of the class
   */
  public CommonsMultipartFileEditor() {

    super();
  }
  
  @Override
  public String getAsText() {

    return format((CommonsMultipartFile) getValue());
  }
  
  @Override
  public void setAsText(String text) {

    setValue(null);
  }
  
  /**
   * Formats the passed multipart file
   * 
   * @param multipartFile The multipart file
   * @return The string representation of multipart file.
   */
  public static String format(CommonsMultipartFile multipartFile) {

    String label = "";
    
    if (multipartFile != null) {
      
      label = multipartFile.getOriginalFilename();
    }
    
    return label;
  }
  
}
